package net.code.btalk.ui;



import net.code.btalk.BTalk;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.CheckboxField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.PasswordEditField;
import net.rim.device.api.ui.component.TextField;
import net.rim.device.api.ui.container.MainScreen;

public class LoginScreen extends MainScreen {
	
	public TextField usernameField;
	public PasswordEditField passwordField;
	
	public CheckboxField saveField;
	private ButtonField loginButton;
	public static BTalk btalk;
	
	public LoginScreen(boolean saved, String username, String password){
		this.add(new LabelField("Username:"));
		usernameField = new TextField(TextField.NO_COMPLEX_INPUT | TextField.NO_NEWLINE);
		this.add(usernameField);
		this.add(new LabelField("Password:"));
		passwordField = new PasswordEditField();
		this.add(passwordField);
		
		saveField = new CheckboxField("Save username&password", saved);
		this.add(saveField);
		
		this.add(loginButton = new ButtonField("Login") {
			protected boolean keyChar(char key, int status, int time) {
				if (key == Keypad.KEY_ENTER) {
					if (usernameField.getText().length() == 0 ||
							passwordField.getText().length() == 0) {
						Dialog.alert("Invalid username/password!");
						return true;
					}
						
					btalk.loginJxa(usernameField.getText(), passwordField.getText());
					return true;
				} else {
					return false;
				}
			}
		});
		
		this.addMenuItem(new MenuItem("Options", 0, 0){
			public void run() {
				btalk.pushScreen(new OptionScreen());
			}
		});
		
		if (saved) {
			usernameField.setText(username);
			passwordField.setText(password);
			loginButton.setFocus();
		}
	}
	
	public boolean onClose() {
		this.close();
		return true;
	}

}
